

using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Service
{
    public class GiftService : ServiceBase<Gift>
    {
        const string APIURL_INSERT = "/webservice/gift/insert.html";
        const string APIURL_UDPATE = "/webservice/gift/update.html";
        const string APIURL_DELETE = "/webservice/gift/delete.html";

        public int Insert(Gift gift)
        {
            IDictionary<string, object> para = new Dictionary<string, object>();
            IList<string> fileNames = new List<string>();
            para.Add("name", gift.Name);
            para.Add("price", gift.Price.ToString("F2"));
            para.Add("buyInPrice", gift.BuyInPrice.ToString("F2"));
            if (string.IsNullOrWhiteSpace(gift.Image) == false)
            {
                para.Add("image", File.ReadAllBytes(gift.Image));
                fileNames.Add(gift.Image);
            }

            return this.DoPost<int>(APIURL_INSERT, para, fileNames).Datas[0];
        }

        public void Update(Gift gift)
        {
            IDictionary<string, object> para = new Dictionary<string, object>();
            IList<string> fileNames = new List<string>();
            para.Add("id", gift.Id.ToString());
            para.Add("name", gift.Name);
            para.Add("price", gift.Price.ToString("F2"));
            para.Add("buyInPrice", gift.BuyInPrice.ToString("F2"));
            if (string.IsNullOrWhiteSpace(gift.Image) == false)
            {
                para.Add("image", File.ReadAllBytes(gift.Image));
                fileNames.Add(gift.Image);
            }
            this.DoPost<int>(APIURL_UDPATE, para, fileNames);
        }

        public void Delete(Gift gift)
        {
            IDictionary<string, string> para = new Dictionary<string, string>();
            para.Add("id", gift.Id.ToString());
            this.DoPost<int>(APIURL_DELETE, para);
        }
    }
}
